namespace Hrm.EntityFramework.MigrationsPermission
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Applications",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Navigations",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 20),
                        Name = c.String(maxLength: 20),
                        ParentCode = c.String(maxLength: 20),
                        IsShow = c.Boolean(nullable: false),
                        Order = c.Int(nullable: false),
                        Url = c.String(maxLength: 200),
                        Description = c.String(maxLength: 200),
                        Application_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Applications", t => t.Application_Id)
                .Index(t => t.Application_Id);
            
            CreateTable(
                "dbo.NavigationAndFunctions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Function_Id = c.Int(),
                        Navigation_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Functions", t => t.Function_Id)
                .ForeignKey("dbo.Navigations", t => t.Navigation_Id)
                .Index(t => t.Function_Id)
                .Index(t => t.Navigation_Id);
            
            CreateTable(
                "dbo.Functions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 20),
                        Name = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Roles",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 20),
                        Description = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Users",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 20),
                        Password = c.String(maxLength: 20),
                        Telephone = c.String(maxLength: 20),
                        StaffId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Departments",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 20),
                        ParentId = c.Int(nullable: false),
                        Order = c.Int(nullable: false),
                        Company_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Companies", t => t.Company_Id)
                .Index(t => t.Company_Id);
            
            CreateTable(
                "dbo.Companies",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 20),
                        Description = c.String(maxLength: 200),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Staffs",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ChineseName = c.String(),
                        EnglishName = c.String(),
                        Adddress = c.String(),
                        WorkNumber = c.String(),
                        Telephone = c.String(),
                        Email = c.String(),
                        Department_Id = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Departments", t => t.Department_Id)
                .Index(t => t.Department_Id);
            
            CreateTable(
                "dbo.RoleNavigationAndFunctions",
                c => new
                    {
                        Role_Id = c.Int(nullable: false),
                        NavigationAndFunction_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Role_Id, t.NavigationAndFunction_Id })
                .ForeignKey("dbo.Roles", t => t.Role_Id, cascadeDelete: true)
                .ForeignKey("dbo.NavigationAndFunctions", t => t.NavigationAndFunction_Id, cascadeDelete: true)
                .Index(t => t.Role_Id)
                .Index(t => t.NavigationAndFunction_Id);
            
            CreateTable(
                "dbo.UserRoles",
                c => new
                    {
                        User_Id = c.Int(nullable: false),
                        Role_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.User_Id, t.Role_Id })
                .ForeignKey("dbo.Users", t => t.User_Id, cascadeDelete: true)
                .ForeignKey("dbo.Roles", t => t.Role_Id, cascadeDelete: true)
                .Index(t => t.User_Id)
                .Index(t => t.Role_Id);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.Staffs", "Department_Id", "dbo.Departments");
            DropForeignKey("dbo.Departments", "Company_Id", "dbo.Companies");
            DropForeignKey("dbo.UserRoles", "Role_Id", "dbo.Roles");
            DropForeignKey("dbo.UserRoles", "User_Id", "dbo.Users");
            DropForeignKey("dbo.RoleNavigationAndFunctions", "NavigationAndFunction_Id", "dbo.NavigationAndFunctions");
            DropForeignKey("dbo.RoleNavigationAndFunctions", "Role_Id", "dbo.Roles");
            DropForeignKey("dbo.NavigationAndFunctions", "Navigation_Id", "dbo.Navigations");
            DropForeignKey("dbo.NavigationAndFunctions", "Function_Id", "dbo.Functions");
            DropForeignKey("dbo.Navigations", "Application_Id", "dbo.Applications");
            DropIndex("dbo.UserRoles", new[] { "Role_Id" });
            DropIndex("dbo.UserRoles", new[] { "User_Id" });
            DropIndex("dbo.RoleNavigationAndFunctions", new[] { "NavigationAndFunction_Id" });
            DropIndex("dbo.RoleNavigationAndFunctions", new[] { "Role_Id" });
            DropIndex("dbo.Staffs", new[] { "Department_Id" });
            DropIndex("dbo.Departments", new[] { "Company_Id" });
            DropIndex("dbo.NavigationAndFunctions", new[] { "Navigation_Id" });
            DropIndex("dbo.NavigationAndFunctions", new[] { "Function_Id" });
            DropIndex("dbo.Navigations", new[] { "Application_Id" });
            DropTable("dbo.UserRoles");
            DropTable("dbo.RoleNavigationAndFunctions");
            DropTable("dbo.Staffs");
            DropTable("dbo.Companies");
            DropTable("dbo.Departments");
            DropTable("dbo.Users");
            DropTable("dbo.Roles");
            DropTable("dbo.Functions");
            DropTable("dbo.NavigationAndFunctions");
            DropTable("dbo.Navigations");
            DropTable("dbo.Applications");
        }
    }
}
